package com.mydesignpattern.beijing.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 利用jdk的proxy生成代理类的handler
 * Created by Administrator on 2017/5/16 0016.
 */
public class JdkProxyHandler implements InvocationHandler {

    private Object obj;

    public JdkProxyHandler(Object obj){
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object object ;
        System.out.println("方法执行前日志");
        object = method.invoke(this.obj, args);
        System.out.println("方法执行后日志");
        return object;
    }

    public Object getInstance(){
        return (Person) Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
    }

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }
}
